<?php
/*
 * Copyright (c) 2021.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\controller\adminapi\customer;

use think\App;
use app\common\model\user\UserPermission;
use hiduoke\controller\BaseController;
use app\common\model\platform\Platform as PlatformModel;

class Platform extends BaseController
{

    /**
     * @var PlatformModel
     */
    public $model;

    protected $visit_type = 'customer';

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PlatformModel();
    }

    /**
     * 显示平台列表
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function index()
    {
        $my_plaid = $this->model->getPlaids($this->userInfo['uid']);
        $per_plaids = (new UserPermission())->getPlaids($this->userInfo['uid']);
        $plaids = array_merge($my_plaid, $per_plaids);
        $mix = [
            'where' => ['plaid', 'in', $plaids],
            'field' => 'plaid,logo,type,uid,status,name,default_app_id'
        ];
        $_data = $this->model->getPageListData($mix)->append(['type_name', 'type_mark', 'addons']);
        $count = $this->model->getPageListCount($mix);

        $data = [];
        foreach ($_data->toArray() as $key => $value) {
            $addons = $value['addons'];
            foreach ($addons as $k => $val) {
                if($val['addon']){

                $addons[$k] = [
                    'id'         => $val['id'],
                    'plaid'      => $val['plaid'],
                    'aid'        => $val['aid'],
                    'addon_name' => $val['addon_name'],
                    'addon'      => [
                        'icon'        => $val['addon']['icon']??'',
                        'title'       => $val['addon']['title']??'',
                        'default_url' => $val['addon']['default_url']??''
                    ]
                ];
                }

            }
            $value['addons'] = $addons;
            // 前端地址和数据库地址不一致，这里做转换处理
            $value['type_mark'] = $value['type_mark'] == 'min_app'?'app':$value['type_mark'];
            $platform_url = $value['type_mark'] . "?p=" . $value['plaid'];
            if ($value['type'] == 2 && $value['default_app_id']) {
                $platform_url = $platform_url . "&appid=" . $value['default_app_id'];
            }
            $value['platform_url'] = $platform_url;
            $data[] = $value;
        }
        return $this->success($count, $data);
    }

}
